<?

	$DEBUGGING = true;
	$TRACECOUNT = 0;
	 
	if($DEBUGGING)
	{		
		ini_set('display_errors', 'On');
		error_reporting(E_ALL);
	}
	 
	function trace($message)
	 {
		global $DEBUGGING;
		global $TRACECOUNT;
		if($DEBUGGING)
		{
			echo '<hr />;'.$TRACECOUNT++.'<code>'.$message.'</code><hr />';
		}
	}
	 
	function tarr($arr)
	{
		global $DEBUGGING;
		global $TRACECOUNT;
		if($DEBUGGING)
		{
			echo '<hr />'.$TRACECOUNT++.'<code>';
			print_r($arr);
			echo '</code><hr />';
		}
	} 
switch($act){
	case "detail":
		Detail();
		ShowImagesSlider();
		$tpl="detail";
		break;
	
	case "special":
		Special();
		$tpl="special";
		break;	

	case 'search':
		Search();
		$title_page = RESULT_SEARCH;
		$tpl="list";
		break;
	
	case 'favourite':
		Favourite();
		$title_page = FAVOURITE_LIST;
		$tpl = 'list';
		break;
	
	default:
		ShowImagesSlider();
		ShowList();
		$tpl="list";
}
function ShowImagesSlider()
{
	$id = isset($_GET['id'])?$_GET['id']:'1';
	$_SESSION['id_slider'] = $id;	
}
function CountArticleView($ArticleId, $db){
	global $UniqueSession;
	$UniqueSession = "imagesAreViewed";
	$ArticleIdString = "[" . $ArticleId . "]";
	
	if(!isset($_SESSION[$UniqueSession]))
	{
		$_SESSION[$UniqueSession] = $ArticleIdString;
		
		$sql = "update images set view = view + 1 where id = $ArticleId";	
		$db->query($sql);
	}
	else
	{
		$mystring = $_SESSION[$UniqueSession];
		$findme = $ArticleIdString;
		$pos = strpos($mystring, $findme);
		
		if($pos === false)
		{
			$_SESSION[$UniqueSession] .= $ArticleIdString;
			
			$sql = "update images set view = view + 1 where id = $ArticleId";	
			$db->query($sql);
		}
	}
}
function ShowList()
{
	global $db,$images,$page,$plpage,$set_per_page,$c, $cat,$title_bar, $idMenuMain, $newer, $older, $product, $FullUrl, $big_images;	
	global $cat, $cat1, $cat2, $lg, $prefix_url, $title_page;
	
	if(isset($cat2)){
		$cat = $cat2;
		$title_bar =  "<a title='".$cat1["name_$lg"]."' href='" . $FullUrl . $prefix_url .$cat1["unique_key_$lg"]. "/'>" .$cat1["name_$lg"]. "</a> > <a title='".$cat2["name_$lg"]."' href='" . $prefix_url .$cat1["unique_key_$lg"]. "/" .$cat2["unique_key_$lg"]. "/'>" .$cat2["name_$lg"]. "</a>";
	}else{
		$cat = $cat1;
		$title_bar =  "<a title='".$cat1["name_$lg"]."' href='" . $FullUrl . $prefix_url .$cat1["unique_key_$lg"]. "/'>" .$cat1["name_$lg"]. "</a>";
	}
	
	$title_page = $cat["title_$lg"]; 
	
	$set_per_page = CST_ARTICLE_PAGING_LIST;
	
	if($cat['id'] != '')
		$sql="select *, (select count(cmt_id) from comments where cmt_article_id = id) as num_of_comments from image_post_silder_id where name_$lg<>'' and  active=1 and cid=".$cat['id']." order by num asc, id desc";
	else
		$sql="select *, (select count(cmt_id) from comments where cmt_article_id = id) as num_of_comments from image_post_silder_id where name_$lg<>'' and  active=1 order by num asc, id desc";
	//echo $sql;
	$c = $db->numRows($db->query($sql));
	$plpage = plpage_seo($sql,$page,$set_per_page);
	$sqlstmt = sqlmod($sql,$page,$set_per_page);
	$images = $db->getAll($sqlstmt);	 //echo $sqlstmt;
}

function Detail()
{
	global $db, $images, $id_article, $cat, $another_news, $title_bar, $idMenuMain, $page, $plpagecomment, $set_per_page;
	global $cat1, $cat2, $lg, $prefix_url, $title_page, $keywords, $descriptions, $FullUrl, $news_comments, $tpl;
	
	if(isset($cat2)){
		$cat = $cat2;
		$title_bar =  "<a title='".$cat1["name_$lg"]."' href='" . $FullUrl . $prefix_url .$cat1["unique_key_$lg"]. "/'>" .$cat1["name_$lg"]. "</a> > <a title='".$cat2["name_$lg"]."' href='" . $prefix_url .$cat1["unique_key_$lg"]. "/" .$cat2["unique_key_$lg"]. "/'>" .$cat2["name_$lg"]. "</a>";
	}else{
		$cat = $cat1;
		$title_bar =  "<a title='".$cat1["name_$lg"]."' href='" . $FullUrl . $prefix_url .$cat1["unique_key_$lg"]. "/'>" .$cat1["name_$lg"]. "</a>";
	}
		
	$unique_key = CleanSQLInjection(trim(isset($_GET['unique_key'])?$_GET['unique_key']:''));
	$images_key = $unique_key;
	$sql = "select *, (select count(cmt_id) from comments where cmt_article_id = id) as num_of_comments from image_post_silder_id where unique_key_$lg='$images_key' and cid=" . $cat['id'];
	$images = $db->getRow($sql);
	//echo $sql;
	/*
	CountArticleView($images['id'], $db);
	//seo
	$title_page = $images["title_$lg"];
	$keywords = $images["keyword_$lg"];
	$descriptions = $images["des_$lg"];
	
	$sql = "select *, (select count(cmt_id) from comments where cmt_article_id = id) as num_of_comments from articles where id<" . $images["id"] . " and name_$lg<>'' and cid=".$images['cid']." order by num asc, id desc limit 0,4";
	$another_news = $db->getAll($sql);
	
	$set_per_page = CST_ARTICLE_PAGING_COMMENT;
	$sql = "select *, (select name from member where id = cmt_mem_id) as mem_name from comments where cmt_article_id = ".$images['id']." and cmt_active = 1 order by cmt_id desc";
	
	$plpagecomment = plpage_seo($sql,$page,$set_per_page);
	$sqlstmt = sqlmod($sql,$page,$set_per_page);
	$news_comments = $db->getAll($sqlstmt);
	//echo $tpl.'3';*/
}
?>